import Vue from 'vue'
import Router from 'vue-router'
import mains from './mains'

Vue.use(Router)

let Routes = []

const addRoutes = (routes) => {
  routes.forEach(route => {
    Routes.push({
      ...route,
      name: route.component.default.name,
      meta: {
        ...route.meta,
        login: route.component.default.login
      },
      component: resolve => resolve(route.component)
    })
  })
}

addRoutes(mains)

const router = new Router({
  mode: 'history',
  routes: Routes,
  scrollBehavior (to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})

export default router
